#include "ParseUtil.h"




unsigned int getIntFrom4Byte(char* input){


	return getIntFromByte(input,4);
	
}


unsigned int getIntFromByte(char* input,int size)
{
	int ret = 0;
	int i = 0;

	int shift = 0;

	for( i = 0 ; i < size ; i ++ )
	{
		shift = (size -1 - i) * 8;
		ret +=  (*input++ &0xFF) << shift;
	}
	return ret;
}

unsigned int getIntFrom4Byte_R(char* input){


	return getIntFromByte_R(input,4);

	
}


unsigned int getIntFromByte_R(char* input,int size)
{
	int ret = 0;
	int i = 0;

	int shift = 0;

	for( i = 0 ; i < size ; i ++ )
	{
		shift = (i) * 8;
		ret +=  (*input++ &0xFF) << shift;
	}
	return ret;
}

unsigned char* readChars(const int size,FILE* fp)
{
	unsigned char* BUFFER;

	BUFFER = (unsigned char*)malloc(sizeof(unsigned char) * size);

	fread(BUFFER,sizeof(unsigned char),(size_t)size,fp);

	return BUFFER;


}














int dellocateMeta(struct Meta* meta)
{

	if(meta != NULL )
	{
		if(meta->ProtocolInfo != NULL)
		{
			free(meta->ProtocolInfo);
		}

		if(meta->AUD_CODEC != NULL)
		{
			free(meta->AUD_CODEC);
		}
		if(meta->VID_CODEC != NULL)
		{
			free(meta->VID_CODEC);
		}
		if(meta->title != NULL)
		{
			free(meta->title);
		}
		if(meta->author != NULL)
		{
			free(meta->author);
		}
		if(meta->upnpClass != NULL)
		{
			free(meta->upnpClass);
		}


	}

}

long GetFileLength(FILE* fp){
	long size;
	
	fseek(fp,0L,SEEK_END);
	size = ftell(fp);
	fseek(fp,0L,SEEK_SET);

	return size;
}



int getFileTitle(char* filePath, char* title, char* ext){

	char FilePathBuffer[256];
	
	char* FileName;
	char* _Ext;

	char* NEXT_TOK;

	
	strcpy(FilePathBuffer,filePath);
	FileName = strtok(FilePathBuffer,"/");
	while(FileName != NULL)
	{
		NEXT_TOK = strtok(NULL,"/");
		
		if(NEXT_TOK == NULL) {
			break;
		}
		
		FileName = NEXT_TOK;
	}
	strcpy(title,FileName);

	strcpy(FilePathBuffer,filePath);
	_Ext = strtok(FileName,".");
	while(_Ext != NULL)
	{
		NEXT_TOK = strtok(NULL,".");

		if(NEXT_TOK==NULL)
			break;
			
		_Ext = NEXT_TOK;
	}
	strcpy(ext,_Ext);


}

int getStringFromBuffer(char* input, char* output, int size)
{
	int i =0 ;
	int i_buff=0;

	char Buffer[256]={0};


	for(i = 0 ; i < size ; i++)
	{

		if(*input == 0 )
		{
			*input++;
			continue;
		}
		Buffer[i_buff++] = *input++;
	}
	

	strcpy(output,Buffer);

	return 0;
}



int ConvertDuration(int dd, char* ret)
{
	char Duration[20];
	int s;
	int r;
	int h;
	int h2;

	if(dd < 60)
	{
		if(dd< 10)
			sprintf(Duration,"0:00:0%d",dd);
		else
			sprintf(Duration,"0:00:%d",dd);
	}else if (dd <3600)
	{
		s = dd/60;
		r = dd%60;

		if(s < 10)
		{
			if(r < 10)
			{
				sprintf(Duration,"0:0%d:0%d",s,r);
			}else
				sprintf(Duration,"0:0%d:%d",s,r);
			}else
			{
				if(r < 10)
				{
					sprintf(Duration,"0:%d:0%d",s,r);
				}else
					sprintf(Duration,"0:%d:%d",s,r);
			}
			
		}else
		{
			h = dd / 3600;
			h2 = dd % 3600;
			s = h2/60;
			r = h2%60;

			if(s <10)
			{
				if(r < 10)
				{
					sprintf(Duration,"%d:0%d:0%d",h,s,r);
				}else
				{
					sprintf(Duration,"%d:0%d:%d",h,s,r);

				}

			}else
			{
				if(r < 10)
				{
					sprintf(Duration,"%d:%d:0%d",h,s,r);
				}else
				{
					sprintf(Duration,"%d:%d:%d",h,s,r);

				}
			}
			
		}

	strcpy(ret,Duration);
	return 0;

}